#include "xgmm_custom.h"
#include "ui_xgmm_custom.h"
#include "dlcw.h"
#include "value.h"
#include <QMessageBox>
#include "dialoglogin.h"
extern std::string _name;
xgmm_custom::xgmm_custom(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::xgmm_custom)
{
    ui->setupUi(this);
    ui->username->setText(QString::fromStdString(_name));
    setWindowTitle("修改密码");
}

xgmm_custom::~xgmm_custom()
{
    delete ui;
}

void xgmm_custom::on_yesButton_clicked()
{
    Accp mp, head;
    FILE* fp;
    head = ff();
    mp=head;
    std::string ad="admin123";
    std::string old_password = ui->pswd_old->text().trimmed().toStdString();
    std::string new_password = ui->pswd_new->text().trimmed().toStdString();
    std::string cfm_password = ui->pswd_confirm->text().trimmed().toStdString();
    if(strlen(old_password.c_str())==0||strlen(new_password.c_str())==0){
        QMessageBox::warning(this,"err","密码不能为空");
        return;
    }
    if(strlen(new_password.c_str())<6||strlen(new_password.c_str())>10){
        QMessageBox::warning(this,"密码长度问题","请输入6-10位的密码");
        return;
    }
    if(strcmp(_name.c_str(),ad.c_str())==0){
        QMessageBox::warning(this,"err","修改管理员密码请联系开发人员.联系方式：135xxxx7813");
        return;
    }
    while (mp != NULL && strcmp(mp->name,_name.c_str()) != 0)//遍历链表，寻找当前登陆账户的账号
    {
        mp = mp->next;
    }
    if (strcmp(mp->password, old_password.c_str()) == 0)          //如果旧密码输入正确
    {
        if(strcmp(new_password.c_str(),cfm_password.c_str())!=0) QMessageBox::warning(this,"err","新密码不一致，请检查！");
        strcpy(mp->password, new_password.c_str());         //将旧密码改为新密码
        QMessageBox::information(this," ","修改成功");
    }
    else                                         //如果旧密码输入错误
    {
        QMessageBox::warning(this,"err","密码不正确，请重试！");
        return;
    }
    mp = head->next;                           //使p指向第二个结点
    fp = fopen("account", "w");                  //先清空原文件内容再将修改后的信息写入文件
    if (fp == NULL)
    {
        //printf("cannot open file\n");
        QMessageBox::warning(this,"err","打开文件失败！");
        return;
    }
    if (fwrite(head, N, 1, fp) != 1)                 //将head所指向的数据写入fp所指向的文件
    {
        //printf("file write error\n");
        QMessageBox::warning(this,"err","写入文件失败！");
        return;
    }
    fclose(fp);                         //关闭文件
    fp = fopen("account", "a");          // 打开account文件，方式为追加
    while (mp != NULL)                    //p不为空就执行循环体
    {
        if (fwrite(mp, N, 1, fp) != 1)            //将p所指向的1个大小为N的数据结构写入fp所指向的文件
        {
            //printf("file write error\n");
            QMessageBox::warning(this,"err","写入文件失败！");
            return;
        }
        mp = mp->next;                     //使p指向下一个结点
    }
    fclose(fp);              //关闭文件
    accept();
}
